<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Validator;

class AuxiliaryCheckUpdateRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    public function withValidator(Validator $validator)
    {
        $validator->after(function (Validator $validator) {
            $images_count = $this->input('images') ? count($this->input('images')) : 0;
            $files_count = $this->file('image_file') ? count($this->file('image_file')) : 0;

            if (($images_count + $files_count) > 9) {
                $validator->messages()->add('image_file', '图片数量超过限制。');
            }
        });
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'images'     => 'array|min:0|max:9|nullable',
            'image_file' => 'array|min:0|max:9|nullable',
            'remark' => 'max:500|nullable',
        ];
    }
}
